import { getRegularFabricList } from '@/api/purchase/fabric.js';
import { getMobileInboundDict } from '@/api/system/dict.js';

const purchase = {
	state: {
		productList: [],
		mobileInboundList: [],
	},

	mutations: {
		SET_PRODUCT_LIST: (state, productList) => {
			state.productList = productList;
		},
		setMobileInboundList: (state, list) => {
			state.mobileInboundList = list;
		},
	},

	actions: {
		FetchProductList({ commit }) {
			getRegularFabricList().then((res) => {
				const { rows } = res;
				let productList = [];
				if (Array.isArray(rows)) {
					productList = rows.map((item) => ({
						value: item.productCode,
						text: item.productName,
						...item,
					}));
				} else {
					productList = [];
				}
				commit('SET_PRODUCT_LIST', productList);
			});
		},
		FetchMobileInbound({ commit }) {
			getMobileInboundDict().then((res) => {
				const { data } = res;
				const list = data?.map((item) => ({
					value: item.dictValue,
					text: item.dictLabel,
					...item,
				}));
				commit('setMobileInboundList', list || []);
			});
		},
	},
};

export default purchase;
